/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.course.excel;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springblade.common.utils.SnowFlakeUtil;
import org.springblade.core.excel.support.ExcelImporter;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.modules.course.entity.CourseChapterEntity;
import org.springblade.modules.course.entity.CourseChapterSubEntity;
import org.springblade.modules.course.entity.CourseEntity;
import org.springblade.modules.course.service.ICourseChapterService;
import org.springblade.modules.course.service.ICourseChapterSubService;
import org.springblade.modules.course.service.ICourseService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 批量导入课程目录
 *
 * @author junxin
 */
@RequiredArgsConstructor
public class CourseImporter implements ExcelImporter<CourseExcel> {

	private final ICourseService courseService;
	private final ICourseChapterService courseChapterService;
	private final ICourseChapterSubService courseChapterSubService;

	@Override
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public void save(List<CourseExcel> dataList) {
		if (CollectionUtil.isEmpty(dataList)) return;

		List<CourseChapterSubEntity> dataSubList = new ArrayList<>();
		//根据课程名称分组
		Map<String, List<CourseExcel>> courseMap = dataList.stream().collect(Collectors.groupingBy(CourseExcel::getCourseName));
		List<CourseEntity> courseSortList = courseService.list(Wrappers.<CourseEntity>query().lambda().orderByDesc(CourseEntity::getSort));
		int courseSort=1;
		if (CollectionUtils.isNotEmpty(courseSortList)
			&& null!=courseSortList.get(0).getSort()
		    && courseSortList.get(0).getSort()>=1) {

			courseSort=courseSortList.get(0).getSort()+1;
		}


		for (String courseName : courseMap.keySet()) {
			List<CourseExcel> courseList = courseMap.get(courseName);
//		courseMap.forEach((courseName, courseList) -> {
			//1.新增课程
			CourseEntity courseEntity = new CourseEntity();
			courseEntity.setName(courseName);
			courseEntity.setSort(courseSort++);
			courseService.save(courseEntity);

			//2.新增章节
			Map<String, List<CourseExcel>> chapterMap = courseList.stream().collect(Collectors.groupingBy(CourseExcel::getChapterName));
			int chapterSort=1;
			for (String chapterName : chapterMap.keySet()) {
				List<CourseExcel> chapterList = chapterMap.get(chapterName);
				CourseChapterEntity chapterEntity = new CourseChapterEntity();
				chapterEntity.setCourseId(courseEntity.getId());
				chapterEntity.setChapterName(chapterName);

				if ( CollectionUtil.isNotEmpty(chapterList) && null!=chapterList.get(0).getChapterSort()){
					chapterEntity.setSort(chapterList.get(0).getChapterSort());//此章节的任意一行的这个参数都是一样的
				}else {
					chapterEntity.setSort(chapterSort);
				}
				courseChapterService.save(chapterEntity);
				chapterSort++;

				//3.新增小节
				for (CourseExcel sub : chapterList) {
					if (null == sub.getSubName()) return;
					CourseChapterSubEntity subEntity = new CourseChapterSubEntity();
					subEntity.setCourseId(courseEntity.getId());
					subEntity.setChapterId(chapterEntity.getId());
					subEntity.setSort(sub.getSubSort());
					subEntity.setSubName(sub.getSubName());
					dataSubList.add(subEntity);
				}
			}
		}
		courseChapterSubService.saveBatch(dataSubList);
	}


}
